package servlets;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modelo.Cancha;
import modelo.Cliente;
import dao.LocalDAO;
import excepcion.DAOExcepcion;

/**
 * Servlet implementation class CanchasLocalServlet
 */
@WebServlet("/CanchasLocalServlet")
public class CanchasLocalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CanchasLocalServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession sesion = request.getSession();
		if ((Cliente) sesion.getAttribute("cliente") != null) {
			int idLocal =  Integer.parseInt((String) request.getParameter("idLocal"));
			String nombre = (String) request.getParameter("nombre");
			
			try {
				Collection<Cancha> canchas = (Collection<Cancha>) ((new LocalDAO()).ListarCanchas(idLocal));
				request.setAttribute("canchas", canchas);
				request.setAttribute("nombre", nombre);
			} catch (DAOExcepcion e) {
				e.printStackTrace();
			}
			RequestDispatcher vista = request.getRequestDispatcher("ListaCanchas.jsp");
            vista.forward(request, response);
		} else {
			response.sendRedirect("Logeo.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
